package edu.mercer.medapps.documentbldr;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.common.collect.ImmutableMap;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * DocumentProperties data class.<p>
 * 
 * Single object providing storage, validation, access to Document's 
 * properties.<p>
 * 
 * Note 'Z' as in Document "Z properties()" requirement.<p>
 * 
 * @see Document
 */

class DocumentProperties implements Document<ImmutableMap<String,String>>
{
	private final Map<String,String> _features = new HashMap<String,String>();
	
	
	DocumentProperties()
	{
		initialized();
		
	}//constructor

	
	DocumentProperties clear()
	{
		return initialized();
		
	}//method

	
	private DocumentProperties initialized()
	{
		_features.clear();
		
		final Iterator<String> featuresIter = FEATURES
											  .SET
											  .EXPOSED
											  .iterator();
		while( featuresIter.hasNext() )
		{
			_features.put(featuresIter.next(), QUALITIES
											   .EMPTY
											   .text());
			
		}//while
		
		return this;
		
	}//method
	
	
	@Fulfills("Document")
	public ImmutableMap<String,String> properties()
	{
		return ImmutableMap.copyOf(this._features);
		
	}//method
	
	
	public String retrieve(final String keyIn)
	{
		return this._features
				   .get(keyIn);
		
	}//method
	
	
	public DocumentProperties replace(final String keyIn, final String valueIn)
	{
		checkNotNull( keyIn  , 
				"null detected document property key argument" );
		
		checkNotNull( valueIn, 
				"null detected document property value argument" );
		
		checkArgument( this._features
						   .containsKey(keyIn), 
						   "unknown document property : " + keyIn );
		
		_features.put( keyIn, valueIn );
		
		checkArgument( this._features
						   .get(keyIn)
						   .equals(valueIn), 
				"failure to set key : " + keyIn + " to value : " + valueIn );
		
		return this;
		
	}//method
	
	
}//class
